<?php
namespace Home\Controller;
use Think\Controller;
class CartController extends Controller {
    public function index(){


    	if (empty(session('suser_id'))) {
    		
    		$this->error('尚未登录,请先登录',U('Home/index/index'));
    	}


    		$cart  	= M('cart');

    		$data 	= $cart->field('ct.num as anum,ct.good_id,sg.sku_id,pro.prodectsname,sg.logo,sg.price,pro.max_id' )->alias('ct')
			    			->join('__SKU_GOODS__ sg ON sg.sku_id = ct.good_id')
			    			->join('__PRODUCT__ pro ON pro.max_id = sg.proudct_id')
			    			->where('uid='.session('suser_id'))
			    			->select();

            

            if (!empty($data))
             {
                
                foreach ($data as $key => $value) 

                {
                
                 $total[] = $value['anum'] * $value['price'];

                 $totalprice = array_sum($total);
                 $count      = count($data);
                }

            }
                            
		

			
			// var_dump($count)
			// NSLog($data);
			// NSLog($total);
			$this->assign('totalprice',$totalprice);
			$this->assign('count',$count);
			$this->assign('data',$data);

            if (empty($data)) {
                
                // $this-display('Cart:cart');
                 $this->display('cart/add');
                exit;
            }else if (!empty($data)) {
                
                $this->display('cart/add');
            }

    		
    }



    public function add(){

    	if (empty(session('suser_id')))
         {
    		
    		$this->error('尚未登录,请先登录',U('Home/index/index'));
    	}


    	if (empty($_GET['sku'])||empty($_GET['max_id'])) 
    	{
    		
    		$this->error('您访问的网页不存在',U('Home/index/index'));
    	}


    	$cart 	= M('cart');

    	$sku 	= M('sku_goods');

    	$god 	=$cart->where('good_id='.I('get.sku'))->find();

    	if (empty($god))
         {
 
    		$good 	= $sku->alias('sg')

			->join('__PRODUCT__ pro ON pro.max_id = sg.proudct_id')
			->join('__SKU_CAT__ st ON st.sku_id = sg.sku_id')->where('sg.sku_id='.I('get.sku') AND 'uid='.session('suser_id'))
			->find();

			// NSLog($good);
			// NSLog(session('suser_id'));
		$time = date('Y-m-d H:i:s',time());

		$data 	= ['num'=>1,'good_id'=>I('get.sku'),'uid'=>session('suser_id'),'atime'=>$time];

			if ($cart->add($data) >0) 
					{
						
			// echo $sku->getlastSql();
			// 		exit();
					$this->success('加入购物车成功',U('Home/cart/index'));
					
					}else{

						$this->error('数据异常稍后再试',U('Home/index/index'));
					}

    	}else{

    		$num  = $god['num'] +1;

    		// good_id'=>I('get.sku'),
    		$data 	= ['num'=>$num,'uid'=>session('suser_id')];

    		if ($cart->where('good_id='.I('get.sku') AND 'uid='.session('suser_id'))->save($data)>0) {
    				
    				$this->success('加入购物车成功',U('Home/cart/index'));
					
					}else{

						$this->error('数据异常稍后再试',U('Home/index/index'));
					}
    	}   

    }


    public function updateNum(){

    	if (IS_AJAX) 
    	{


	    	if (empty($_POST['num']) || empty($_POST['sku']) || empty(session('suser_id'))) 
	    	{
	    		
	    			$this->error('您请求的页面不存在',U('Home/index/index'));
	    	}

	    	$cart  	= M('cart');


    		$data 	= ['num'=>I('post.num'),'uid'=>session('suser_id')];

    		$map['good_id'] = array('eq',I('post.sku'));

    		$map['uid'] = array('eq',session('suser_id'));

    				if ($cart->where($map)->save($data)>0) 
    				{
    				
    				$this->ajaxReturn(1);
					
					}else{
						

						$this->ajaxReturn(0);
					}
    		
    	}else{

    		$this->error('您请求的页面不存在',U('Home/index/index'));
    	}
 	
    }




    public function deleteGoods()
    {
    	 if (IS_AJAX) 
    	 {
    		
    		if (empty($_POST['sku'])) 
    		{
    				
    				$this->error('您请求的页面不存在',U('Home/index/index'));
    			
    		}else{

    				$cart  	= M('cart');

    				$falg 	= $cart->where('good_id='.$_POST['sku'])->delete();

    				if ($falg>0)
    				{
    					$this->ajaxReturn(1);
    				}else{
    					$this->ajaxReturn(0);
    				}
    			 }

    	 }else{

    	 	$this->error('您请求的页面不存在',U('Home/index/index'));

    	 }
    }





}
